package networking;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.util.StringTokenizer;

import perceptron.Perceptron;
import perceptron.Results;

/**
 * Cette classe sert juste à tester l'idée
 * @author simon
 *
 */
public class NetworkingTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			
			/* Prendre le percetron de wikipedia */
			Perceptron pWiki = new Perceptron() ;
			pWiki.load("data/wiki1/per") ;
			
			/* Creation du "serveur" ^^ */
			ServerSocket ss = new ServerSocket(55666) ;
			
			while(true) {
				/* Attente d'une demande */
				Socket connexion = ss.accept() ;
			
				/* Récuperation du reader et du writer */
				BufferedReader in = new BufferedReader(new InputStreamReader(connexion.getInputStream()));
				PrintStream out = new PrintStream(connexion.getOutputStream());
			
				/* Lire la requête */
				String line = in.readLine() ;
				System.out.println("--- " + line);
				
				StringTokenizer st = new StringTokenizer(line) ;
				String cmd="", arg="",ver="" ;
				cmd = st.nextToken() ;
				arg = st.nextToken() ;
				ver = st.nextToken() ;
				
				/* Analyser la page demandée */
				Results result = pWiki.classifyPage(arg.substring(1)) ;
				System.out.println(result.getScore());
				
				
				/* Répondre au client */
				boolean javascript = true ;
				out.println(ver+" 200 OK") ;
				out.println() ;
				if(!javascript)
					out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\" ><head><title>Analyse</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" /></head><body><p>La page a un score de "+result+"</p></body></html>") ;
				else
					//out.print("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><response><a>textA</a><b><bb>textBB</bb>textB</b></response>") ;
					out.print(result.getScore()+", classe : "+result.getClass_()) ;
				out.close();
				in.close();
			}
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
